package com.zcx.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zcx.project.entity.ZcxTrainInfo;
import com.zcx.project.mapper.ZcxTrainInfoMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcx.project.entity.ZcxProblem;
import com.zcx.project.mapper.ZcxProblemMapper;


@Controller
@RequestMapping("/ZcxProblem")
public class ZcxProblemController {

    @Autowired
    ZcxProblemMapper zcxProblemMapper;
    @Autowired
    ZcxTrainInfoMapper zcxTrainInfoMapper;


    @RequestMapping(value = "ZcxProblemHtml")
    public ModelAndView getMainPage(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxProblem");
        return mv;
    }


    @RequestMapping(value = "ZcxProblemLDHtml")
    public ModelAndView ZcxProblemLDHtml(HttpSession session, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("zcxProblem-ld");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/findList")
    public List findList(ZcxProblem obj) {
        List list = zcxProblemMapper.findList(obj);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "/query")
    public Map<String, Object> findZcxProblemPage(int currentPage, int rows, ZcxProblem obj) {

        //使用分页插件,核心代码就这一行，页数、每页行数
        PageHelper.startPage(currentPage, rows);
        List<ZcxProblem> list = zcxProblemMapper.findList(obj);
        PageInfo<ZcxProblem> pageInfo = new PageInfo<ZcxProblem>(list);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageInfo.getTotal());
        result.put("rows", pageInfo.getList());
        return result;


    }


    @ResponseBody
    @RequestMapping(value = "/insertZcxProblem")
    public Map<String, Object> addZcxProblem(ZcxProblem obj) {
        int i = zcxProblemMapper.insertSelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "添加失败！");
        else {
            ZcxTrainInfo zt = new ZcxTrainInfo();
            zt.setId(obj.getTrainInfoId());
            zt.setStatus("维修中");
            zcxTrainInfoMapper.updateByPrimaryKeySelective(zt);

            returnMap.put("showInfo", "添加成功！");
        }
        return returnMap;
    }


    @ResponseBody
    @RequestMapping(value = "/updateZcxProblem")
    public Map<String, Object> updateZcxProblem(ZcxProblem obj) {
        int i = zcxProblemMapper.updateByPrimaryKeySelective(obj);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "修改失败！");
        else
            returnMap.put("showInfo", "修改成功！");
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteZcxProblem")
    public Map<String, Object> deleteZcxProblem(int id) {
        int i = zcxProblemMapper.deleteByPrimaryKey(id);
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (i == 0)
            returnMap.put("showInfo", "删除失败！");
        else
            returnMap.put("showInfo", "删除成功！");
        return returnMap;
    }


}
